import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sptapp/api/interface/user_api.dart';
import 'package:sptapp/api/request/response_data.dart';
import 'package:sptapp/config/const.dart';
import 'package:sptapp/widget/common_widget.dart';

import '../../moudle/location/location_tool.dart';
import '../../widget/circle_slider.dart';
import '../test/season.dart';

class MinChooseAddressPage extends StatefulWidget {
  final Map? arguments;
  const MinChooseAddressPage({super.key, this.arguments});

  @override
  State<MinChooseAddressPage> createState() => _MinChooseAddressPageState();
}

class _MinChooseAddressPageState extends State<MinChooseAddressPage> {

  final _locationService = LocationService();
  final _geocodingService = GeocodingService();

  String address = '';

  List states = [];
  String stateId = '';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.arguments != null && widget.arguments!['ID_STATE']!=null){
      stateId = widget.arguments!['ID_STATE'].toString();
    }
    _getCurrentLocation();
    _getStates();
  }

  _getStates() async {
    showLoading();
    ResponseData res = await UserAPI.deviceStateAddress();
    hideLoading();
    if(res.code == 200){
      states = res.data??[];
    }
    if(mounted){
      setState(() {});
    }
  }

  Future<void> _getCurrentLocation() async {
    try {
      final position = await _locationService.getCurrentLocation();
      if(position!=null){
        address = await _geocodingService.getAddressFromLatLng(position!.latitude, position!.longitude);
      }
    } catch (e) {
    }
    if(mounted){
      setState(() {});
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBlackBgColor,
      appBar: myAppBar(context, 'Region'),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 40.w),
        child: Column(
          children: [
            // Visibility(child: GestureDetector(
            //   onTap: (){
            //     _getCurrentLocation();
            //   },
            //   child: Container(
            //     height: 116.w,
            //     padding: EdgeInsets.symmetric(horizontal: 40.w),
            //     decoration: BoxDecoration(
            //         color: Color(0xff1e1d20),
            //         borderRadius: BorderRadius.circular(16.w)
            //     ),
            //     child: Row(
            //       children: [
            //         Image.asset('assets/common/min_ai_location.png',width: 40.w,),
            //         SizedBox(width: 4.w,),
            //         Expanded(child: Text(address??'',style: TextStyle(
            //             color: Colors.white,
            //             fontSize: 28.sp,
            //             fontWeight: FontWeight.w600
            //         ),maxLines: 1,overflow: TextOverflow.ellipsis,)),
            //         SizedBox(width: 24.w,),
            //       ],
            //     ),
            //   ),
            // ),visible: address!=null&&address.length!=0,),
            SizedBox(height: 40.w,),
            Expanded(child: Container(
              padding: EdgeInsets.symmetric(horizontal: 40.w),
              decoration: BoxDecoration(
                  color: Color(0xff1e1d20),
                  borderRadius: BorderRadius.circular(16.w)
              ),
              child: ListView.builder(itemBuilder: (ctx,index){
                Map item = states[index];
                return GestureDetector(
                  onTap: (){
                    setState(() {
                      stateId = item['id'].toString();
                    });
                    if(widget.arguments != null && widget.arguments!['ID']!=null){
                      item['cityId'] = widget.arguments!['ID'].toString();
                    }
                    Navigator.of(context).pushNamed('MinChooseCityPage',arguments: item);
                  },
                  child: Container(
                    height: 116.w,
                    decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1),width: index == 9?0: 1.w))
                    ),
                    child: Row(
                      children: [
                        Expanded(child: Text(item['stateName']??'',style: TextStyle(
                            color: Colors.white,
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w600
                        ),textAlign: TextAlign.left,overflow: TextOverflow.ellipsis,maxLines: 1,)),
                        SizedBox(width: 24.w,),
                        Visibility(child: Text('Selected',style: TextStyle(
                            color: Colors.white,
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w400
                        )),visible: stateId == item['id'].toString(),),
                         SizedBox(width: 6.w,),
                        Image.asset('assets/common/control_right.png',width: 48.w,)
                      ],
                    ),
                  ),
                );
              },itemCount: states.length,padding: EdgeInsets.zero,),
            )),
            SizedBox(height: ScreenUtil().bottomBarHeight,)
          ],
        ),
      ),
    );
  }
}

// class SeasonDemo extends StatefulWidget {
//   const SeasonDemo({Key? key}) : super(key: key);
//
//   @override
//   State<SeasonDemo> createState() => _SeasonDemoState();
// }
//
// class _SeasonDemoState extends State<SeasonDemo> {
//
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       backgroundColor: defaultBlackBgColor,
//       appBar: AppBar(title: const Text('Seasons')),
//       body: Padding(
//         padding: const EdgeInsets.all(16.0),
//         child: Column(
//           children: [
//             Container(
//               width: 320.w,
//               height: 320.w,
//               child: CircleSlider(
//                 initialValue: 0,
//                 activeColor: Color(0xff00c701),
//                 inactiveColor: Color(0xffcd9607),
//                 strokeWidth: 26.w,
//                 thumbSize: 13.w,
//                 thumbColor: Colors.white,
//                 thumbBorderColor: Colors.transparent,
//                 thumbBorderWidth: 0,
//                 overlayColor: Colors.white.withOpacity(0.5),
//                 overlayRadius: 26.w,
//                 onChanged: (value) {
//                   print('Selected value: ${value.round()}');
//                 },
//               ),
//             )
//           ],
//         ),
//       ),
//     );
//   }
// }

